package com.grass.appointment.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.OfficialBean;
import com.androidx.p013lv.base.bean.OfficialListBean;
import com.androidx.p013lv.base.bean.event.ChangeCityEvent;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.grass.appointment.C2694R;
import com.grass.appointment.adapter.OfficialAdapter;
import com.grass.appointment.databinding.FragmentOfficialBinding;
import com.grass.appointment.dialog.AddressDialog;
import com.grass.appointment.model.OfficialModel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class OfficialFragment extends LazyFragment<FragmentOfficialBinding> implements ItemClickListener, OnRefreshLoadMoreListener {
    OfficialAdapter adapter;
    AddressDialog addressDialog;
    String city;
    boolean isResume = false;
    int lastId;
    OfficialModel model;

    public static OfficialFragment newInstance(String str) {
        Bundle bundle = new Bundle();
        bundle.putString(Key.CITY, str);
        OfficialFragment officialFragment = new OfficialFragment();
        officialFragment.setArguments(bundle);
        return officialFragment;
    }

    @Override // androidx.fragment.app.Fragment
    public void setArguments(Bundle bundle) {
        super.setArguments(bundle);
        this.city = bundle.getString(Key.CITY);
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public int setFragmentView() {
        return C2694R.C2699layout.fragment_official;
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public void initViews() {
        EventBus.getDefault().register(this);
        OfficialModel officialModel = (OfficialModel) new ViewModelProvider(this).get(OfficialModel.class);
        this.model = officialModel;
        officialModel.data().observe(this, new Observer<BaseRes<OfficialListBean>>() {
            /* class com.grass.appointment.fragment.OfficialFragment.C28251 */

            public void onChanged(BaseRes<OfficialListBean> baseRes) {
                ((FragmentOfficialBinding) OfficialFragment.this.binding).statusLayout.hideLoading();
                ((FragmentOfficialBinding) OfficialFragment.this.binding).refresh.finishRefresh();
                ((FragmentOfficialBinding) OfficialFragment.this.binding).refresh.finishLoadMore();
                if (baseRes.getCode() == 200) {
                    if (baseRes.getData() == null || baseRes.getData().getList() == null || baseRes.getData().getList().size() <= 0) {
                        if (OfficialFragment.this.lastId == 0) {
                            ((FragmentOfficialBinding) OfficialFragment.this.binding).statusLayout.showEmpty();
                        } else {
                            ((FragmentOfficialBinding) OfficialFragment.this.binding).refresh.finishLoadMoreWithNoMoreData();
                        }
                    } else if (OfficialFragment.this.lastId == 0) {
                        if (!baseRes.getData().getCityName().equals(OfficialFragment.this.city) && OfficialFragment.this.isResume) {
                            ToastUtils.getInstance().showSigh("當前城市無樓鳳信息，看一下全國的小姐姐吧");
                        }
                        OfficialFragment.this.city = baseRes.getData().getCityName();
                        TextView textView = ((FragmentOfficialBinding) OfficialFragment.this.binding).tvAddress;
                        textView.setText("當前位置：" + OfficialFragment.this.city);
                        OfficialFragment.this.adapter.setDomain(baseRes.getData().getDomain());
                        OfficialFragment.this.adapter.setData(baseRes.getData().getList());
                        ((FragmentOfficialBinding) OfficialFragment.this.binding).refresh.resetNoMoreData();
                    } else {
                        OfficialFragment.this.adapter.setDatasInEnd(baseRes.getData().getList());
                    }
                } else if (OfficialFragment.this.lastId == 0) {
                    ((FragmentOfficialBinding) OfficialFragment.this.binding).statusLayout.showError();
                } else {
                    ToastUtils.getInstance().showWeak(baseRes.getMsg());
                }
            }
        });
        ((FragmentOfficialBinding) this.binding).refresh.setOnRefreshListener(this);
        ((FragmentOfficialBinding) this.binding).refresh.setOnLoadMoreListener(this);
        this.adapter = new OfficialAdapter();
        ((FragmentOfficialBinding) this.binding).recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        ((FragmentOfficialBinding) this.binding).recycler.setAdapter(this.adapter);
        ((FragmentOfficialBinding) this.binding).tvSelect.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.appointment.fragment.OfficialFragment.View$OnClickListenerC28262 */

            public void onClick(View view) {
                if (!OfficialFragment.this.isOnClick()) {
                    ARouter.getInstance().build(Constance.CITY_SELECT_ACTIVITY).withInt("type", 0).navigation();
                }
            }
        });
        this.adapter.setItemListener(this);
        TextView textView = ((FragmentOfficialBinding) this.binding).tvAddress;
        textView.setText("當前位置：" + this.city);
        ((FragmentOfficialBinding) this.binding).statusLayout.setOnRetryListener(new View.OnClickListener() {
            /* class com.grass.appointment.fragment.OfficialFragment.View$OnClickListenerC28273 */

            public void onClick(View view) {
                OfficialFragment.this.lastId = 0;
                OfficialFragment.this.getInfo();
            }
        });
        getInfo();
        AddressDialog addressDialog2 = new AddressDialog(getActivity(), new View.OnClickListener() {
            /* class com.grass.appointment.fragment.OfficialFragment.View$OnClickListenerC28284 */

            public void onClick(View view) {
                OfficialFragment.this.addressDialog.dismiss();
                if (!OfficialFragment.this.isOnClick()) {
                    ARouter.getInstance().build(Constance.CITY_SELECT_ACTIVITY).withInt("type", 0).navigation();
                }
            }
        });
        this.addressDialog = addressDialog2;
        addressDialog2.setMessage("定位您當前在" + this.city + "，是否\n切換？");
        this.addressDialog.show();
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onResume() {
        super.onResume();
        this.isResume = true;
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onPause() {
        super.onPause();
        this.isResume = false;
    }

    /* access modifiers changed from: package-private */
    public void getInfo() {
        if (this.lastId == 0) {
            OfficialAdapter officialAdapter = this.adapter;
            if (!(officialAdapter == null || officialAdapter.getData() == null || this.adapter.getData().size() <= 0)) {
                this.adapter.clear();
            }
            if (!NetUtil.isNetworkAvailable()) {
                ((FragmentOfficialBinding) this.binding).statusLayout.showNoNet();
                return;
            }
            ((FragmentOfficialBinding) this.binding).statusLayout.showLoading();
        }
        this.model.getInfo(this.city, this.lastId);
    }

    @Override // com.androidx.p013lv.base.listener.ItemClickListener
    public void onItemClick(View view, int i) {
        if (!isOnClick()) {
            ARouter.getInstance().build(Constance.USER_INFO_ACTIVITY).withInt(Key.USER_ID, ((OfficialBean) this.adapter.getDataInPosition(i)).getUserId()).navigation();
        }
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        OfficialAdapter officialAdapter = this.adapter;
        if (officialAdapter == null || officialAdapter.getData() == null || this.adapter.getData().size() == 0) {
            ((FragmentOfficialBinding) this.binding).refresh.finishLoadMore();
            return;
        }
        this.lastId = ((OfficialBean) this.adapter.getLastDataInPosition()).getUserId();
        getInfo();
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnRefreshListener
    public void onRefresh(RefreshLayout refreshLayout) {
        this.lastId = 0;
        getInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeCityEventClick(ChangeCityEvent changeCityEvent) {
        if (changeCityEvent.getType() == 0) {
            this.city = changeCityEvent.getCityEntity().getName();
            TextView textView = ((FragmentOfficialBinding) this.binding).tvAddress;
            textView.setText("當前位置：" + this.city);
            this.lastId = 0;
            getInfo();
        }
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        OfficialModel officialModel = this.model;
        if (officialModel != null) {
            officialModel.cancelHttp();
        }
    }
}
